package cn.test.future.cf;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 结果消费测试
 */
@Slf4j
public class CompletableFutureTest4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFutureTest4 test4 = new CompletableFutureTest4();
        test4.thenAccept();
        System.out.println("---------------");
        test4.thenAcceptBoth();
        System.out.println("---------------");
        test4.thenRun();
    }

    public void thenAccept() {
        CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(10);
                System.out.println("第一阶段：" + num);
                return num;
            }
        }).thenAccept(number -> {
            System.out.println("第二阶段：" + number);
        });

        System.out.println(future.join());
    }

    public void thenAcceptBoth() {
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(3) + 1;
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("第一阶段：" + num);
                return num;
            }
        });

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt(3) + 1;
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("第二阶段：" + num);
                return num;
            }
        });

        future1.thenAcceptBoth(future2, new BiConsumer<Integer, Integer>() {
            @Override
            public void accept(Integer integer, Integer integer2) {
                System.out.println("最终结果：" + (integer + integer2));
            }
        }).join();
    }

    public void thenRun() {
        CompletableFuture.supplyAsync(new Supplier<Integer>() {
            @Override
            public Integer get() {
                int num = new Random().nextInt();
                System.out.println("第一阶段：" + num);
                return num;
            }
        }).thenRun(new Runnable() {
            @Override
            public void run() {
                System.out.println("thenRun 执行");
            }
        }).join();
    }
}
